package com.schedule.controller;


import com.alibaba.fastjson.JSONObject;
import com.schedule.pojo.Competition;
import com.schedule.pojo.EventArrange;
import com.schedule.pojo.RespBean;
import com.schedule.service.ICompetitionService;
import com.schedule.service.IEventArrangeService;
import com.schedule.service.IGradeService;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author star
 * @since 2022-03-08
 */
@RestController
//@RequestMapping("/grade")
public class GradeController {
    @Autowired
    private IGradeService gradeService;

    /**
     * 根据前端传来的选手ID，比赛ID，分数score三个参数来设置参数
     */
    @ApiOperation(value = "设置分数")
    @PostMapping("/setScore")
    @ResponseBody
    public RespBean setScore(@RequestParam("pid")Integer pid,
                             @RequestParam("cname")String cname,
                             @RequestParam("score") Integer score){
        if(gradeService.setScore(pid, cname, score)){
            return RespBean.success("设置分数成功");
        }
        return RespBean.error("设置分数失败");
    }

    /**
     * 根据前端传来的选手ID，比赛ID，分数score三个参数来更新参数
     */
    @ApiOperation(value = "更新分数")
    @PostMapping("/schedule/grade/updateScore")
    @ResponseBody
    public RespBean updateScore(@RequestBody JSONObject jsonObject){
        String cname = jsonObject.getString("cname");
        Integer pid = jsonObject.getInteger("pid");
        Double score = jsonObject.getDouble("score");
        System.out.println("cname is "+cname);
        if(gradeService.updateScore(pid, cname, score)){
            return RespBean.success("更新分数成功");
        }
        return RespBean.error("更新分数失败");
    }

    /**
     * 根据前端传来的选手ID，比赛ID来删除比赛信息
     */
    @ApiOperation(value = "删除分数")
    @DeleteMapping("/deleteScore")
    @ResponseBody
    public RespBean deleteScore(@RequestParam("pid")Integer pid,
                                @RequestParam("cname")String cname){
        if(gradeService.deleteScore(pid, cname)){
            return RespBean.success("删除分数成功");
        }
        return RespBean.error("删除分数失败");
    }

}
